package com.mrdeer.utils;

import com.mrdeer.common.entity.sys.Message;
import com.mrdeer.common.entity.sys.MessageSend;
import com.mrdeer.web.service.sys.SysMessageSendService;
import com.mrdeer.web.service.sys.SysMessageService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 异步添加消息
 */
@Component
public class AddMessage {

    @Resource
    private SysMessageService sysMessageService;

    @Resource
    private SysMessageSendService sysMessageSendService;

    @Async
    public void addSendMessage(String userId) {
        //获取需要创建账号发送的消息
        List<Message> messages = sysMessageService.findByCreateSend(true);
        List<MessageSend> messageSends = new ArrayList<>();
        messages.forEach(item -> {
            MessageSend ms = new MessageSend();
            ms.setUserId(userId);
            ms.setMessageId(item.getId());
            messageSends.add(ms);
        });
        if (messageSends.size() > 0) {
            sysMessageSendService.saveOrUpdateBatch(messageSends);
        }
    }
}
